const UnionFind = require("../template")

function testUnionFindPerformance() {
  console.log(
    "1. 合并操作性能测试:",
    (() => {
      const uf = new UnionFind(1000)
      const iterations = 500
      
      const start = performance.now()
      
      for (let i = 0; i < iterations; i++) {
        uf.union(i, i + 1)
      }
      
      const end = performance.now()
      const duration = end - start
      
      return duration < 100 && // 应该在100ms内完成
             duration > 0
    })()
  )

  console.log(
    "2. 查找操作性能测试:",
    (() => {
      const uf = new UnionFind(1000)
      
      // 先进行一些合并操作
      for (let i = 0; i < 500; i++) {
        uf.union(i, i + 1)
      }
      
      const start = performance.now()
      
      // 查找操作
      for (let i = 0; i < 1000; i++) {
        uf.find(i)
      }
      
      const end = performance.now()
      const duration = end - start
      
      return duration < 50 && // 查找应该很快
             duration > 0
    })()
  )

  console.log(
    "3. 连通性检查性能测试:",
    (() => {
      const uf = new UnionFind(1000)
      
      // 先进行一些合并操作
      for (let i = 0; i < 500; i++) {
        uf.union(i, i + 1)
      }
      
      const start = performance.now()
      
      // 连通性检查
      for (let i = 0; i < 500; i++) {
        uf.connected(i, i + 1)
      }
      
      const end = performance.now()
      const duration = end - start
      
      return duration < 50 && // 连通性检查应该很快
             duration > 0
    })()
  )

  console.log(
    "4. 批量操作性能测试:",
    (() => {
      const uf = new UnionFind(500)
      const batchSize = 250
      
      const start = performance.now()
      
      // 批量合并
      for (let i = 0; i < batchSize; i++) {
        uf.union(i, i + 1)
      }
      
      // 批量查找
      for (let i = 0; i < batchSize; i++) {
        uf.find(i)
      }
      
      // 批量连通性检查
      for (let i = 0; i < batchSize; i++) {
        uf.connected(i, i + 1)
      }
      
      const end = performance.now()
      const duration = end - start
      
      return duration < 200 &&
             duration > 0
    })()
  )

  console.log(
    "5. 路径压缩性能测试:",
    (() => {
      const uf = new UnionFind(1000)
      
      // 创建长链结构
      for (let i = 0; i < 999; i++) {
        uf.union(i, i + 1)
      }
      
      const start = performance.now()
      
      // 多次查找测试路径压缩
      for (let i = 0; i < 100; i++) {
        uf.find(0)
        uf.find(500)
        uf.find(999)
      }
      
      const end = performance.now()
      const duration = end - start
      
      return duration < 100 && // 路径压缩应该很快
             duration > 0
    })()
  )

  console.log(
    "6. 按秩合并性能测试:",
    (() => {
      const uf = new UnionFind(1000)
      
      const start = performance.now()
      
      // 创建树状结构测试按秩合并
      for (let i = 0; i < 500; i++) {
        uf.union(i * 2, i * 2 + 1)
      }
      
      // 合并不同高度的树
      for (let i = 0; i < 250; i++) {
        uf.union(i * 4, i * 4 + 2)
      }
      
      const end = performance.now()
      const duration = end - start
      
      return duration < 100 &&
             duration > 0
    })()
  )

  console.log(
    "7. 大数据量性能测试:",
    (() => {
      const uf = new UnionFind(10000)
      
      const start = performance.now()
      
      // 插入大量数据
      for (let i = 0; i < 5000; i++) {
        uf.union(i, i + 1)
      }
      
      const end = performance.now()
      const duration = end - start
      
      // 验证数据完整性
      let allCorrect = true
      for (let i = 0; i < Math.min(1000, 5000); i++) { // 抽样检查
        if (!uf.connected(i, i + 1)) {
          allCorrect = false
          break
        }
      }
      
      return allCorrect &&
             duration < 1000 && // 大数据量应该在1秒内完成
             duration > 0
    })()
  )

  console.log(
    "8. 随机操作性能测试:",
    (() => {
      const uf = new UnionFind(1000)
      
      const start = performance.now()
      
      // 随机合并操作
      for (let i = 0; i < 500; i++) {
        const a = Math.floor(Math.random() * 1000)
        const b = Math.floor(Math.random() * 1000)
        uf.union(a, b)
      }
      
      const end = performance.now()
      const duration = end - start
      
      return duration < 200 &&
             duration > 0
    })()
  )

  console.log(
    "9. 连通分量分析性能测试:",
    (() => {
      const uf = new UnionFind(1000)
      
      // 创建连通分量
      for (let i = 0; i < 500; i++) {
        uf.union(i, i + 1)
      }
      
      const start = performance.now()
      
      // 多次分析连通分量
      for (let i = 0; i < 100; i++) {
        uf.getComponentCount()
        uf.getMaxComponentSize()
        uf.getAllComponents()
      }
      
      const end = performance.now()
      const duration = end - start
      
      return duration < 200 && // 分析应该很快
             duration > 0
    })()
  )

  console.log(
    "10. this上下文性能测试:",
    (() => {
      const perfObj = {
        multiplier: 2,
        processPerformance: function(size) {
          const uf = new UnionFind(size)
          const start = performance.now()
          
          for (let i = 0; i < size / 2; i++) {
            uf.union(i, i + 1)
          }
          
          const end = performance.now()
          return (end - start) * this.multiplier
        }
      }
      
      const result = perfObj.processPerformance(1000)
      return result > 0 && result < 1000
    })()
  )

  console.log(
    "11. 复杂对象this绑定性能测试:",
    (() => {
      const perfConfigObj = {
        config: { threshold: 1000 },
        processWithConfig: function(size) {
          const uf = new UnionFind(size)
          const start = performance.now()
          
          for (let i = 0; i < size / 2; i++) {
            uf.union(i, i + 1)
          }
          
          const end = performance.now()
          return (end - start) < this.config.threshold
        }
      }
      
      return perfConfigObj.processWithConfig(2000) === true
    })()
  )

  console.log(
    "12. 性能一致性测试:",
    (() => {
      const uf = new UnionFind(1000)
      
      const start1 = performance.now()
      for (let i = 0; i < 500; i++) {
        uf.union(i, i + 1)
      }
      const end1 = performance.now()
      
      const start2 = performance.now()
      for (let i = 0; i < 500; i++) {
        uf.find(i)
      }
      const end2 = performance.now()
      
      const unionTime = end1 - start1
      const findTime = end2 - start2
      
      return unionTime > 0 &&
             findTime > 0 &&
             findTime < unionTime && // 查找应该比合并快
             unionTime < 100 &&
             findTime < 50
    })()
  )
}

testUnionFindPerformance()
